package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
	"user_srv/app_config"
)

var (
	Db   *gorm.DB
	once sync.Once
	err  error
)

func InitMysql() {
	app := app_config.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", app.User, app.Password, app.Host, app.Port, app.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn))
		if err != nil {
			fmt.Println("mysql数据库连接失败")
			return
		}
	})
	fmt.Println("mysql数据库连接成功")
	Db.AutoMigrate(new(User), new(UserRealName))
}
